{% extends 'base.html' %}

{% block title %}广告主控制面板{% endblock %}

{% block content %}
<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <title>广告主控制面板</title>
    <style>
        /* 主界面样式 */
        body {
            margin: 0;
            padding: 30px;
            font-family: 'Segoe UI', sans-serif;
            background: #f0f2f5;
            min-height: 100vh;
        }

        .dashboard-container {
            max-width: 1200px;
            margin: 0 auto;
            background: white;
            padding: 40px;
            border-radius: 12px;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
        }

        h2 {
            color: #2d3436;
            margin-bottom: 30px;
            font-size: 24px;
        }

        .button-group {
            display: flex;
            gap: 20px;
            margin-bottom: 40px;
            flex-wrap: wrap;
        }

        .contract-button {
            background: #6c5ce7;
            color: white;
            padding: 12px 24px;
            border: none;
            border-radius: 6px;
            font-size: 16px;
            cursor: pointer;
            transition: all 0.3s;
            display: flex;
            align-items: center;
            gap: 8px;
        }

        .contract-button:hover {
            background: #5b4bc4;
            transform: translateY(-1px);
        }

        /* 表格样式 */
        table {
            width: 100%;
            border-collapse: collapse;
            margin: 25px 0;
            box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
        }

        th, td {
            padding: 15px;
            text-align: left;
            border-bottom: 1px solid #eee;
        }

        th {
            background: #f8f9fa;
            font-weight: 600;
        }

        tr:hover {
            background: #f8f9fa;
        }

        .status-indicator {
            padding: 6px 12px;
            border-radius: 20px;
            font-size: 14px;
            display: inline-block;
        }

        .status-pending { background: #fef5e7; color: #f39c12; }
        .status-approved { background: #e8f6f3; color: #27ae60; }
        .status-rejected { background: #fdedec; color: #e74c3c; }

        /* 模态框样式 */
        .modal {
            display: none;
            position: fixed;
            z-index: 1000;
            left: 0;
            top: 0;
            width: 100%;
            height: 100%;
            background: rgba(0,0,0,0.5);
            animation: fadeIn 0.3s ease;
        }

        @keyframes fadeIn {
            from { opacity: 0; }
            to { opacity: 1; }
        }

        .modal-content {
            background: #fff;
            margin: 8% auto;
            padding: 32px;
            border-radius: 12px;
            width: 480px;
            box-shadow: 0 12px 24px rgba(0,0,0,0.2);
            transform: scale(0.9);
            animation: slideIn 0.3s ease forwards;
        }

        @keyframes slideIn {
            to { transform: scale(1); }
        }

        .modal h3 {
            color: #2d3436;
            margin-bottom: 28px;
            font-size: 22px;
            display: flex;
            align-items: center;
            gap: 10px;
        }

        .modal h3::before {
            content: '📄';
            font-size: 1.2em;
        }

        .form-group {
            margin-bottom: 20px;
        }

        .form-group label {
            display: block;
            margin-bottom: 8px;
            color: #555;
            font-weight: 500;
        }

        .form-group input,
        .form-group select,
        .form-group textarea {
            width: 90%;
            padding: 10px 14px;
            border: 2px solid #eee;
            border-radius: 6px;
            font-size: 14px;
            transition: all 0.3s;
        }

        .form-group input:focus,
        .form-group select:focus,
        .form-group textarea:focus {
            border-color: #6c5ce7;
            box-shadow: 0 0 0 3px rgba(108, 92, 231, 0.1);
        }

        .form-group textarea {
            height: 100px;
            resize: vertical;
        }

        .modal button[type="submit"] {
            background: #6c5ce7;
            color: white;
            padding: 12px 28px;
            border: none;
            border-radius: 6px;
            font-size: 16px;
            cursor: pointer;
            transition: all 0.3s;
            width: 100%;
            margin-top: 15px;
        }

        .modal button[type="submit"]:hover {
            background: #5b4bc4;
            transform: translateY(-1px);
        }

        .close {
            color: #aaa;
            font-size: 28px;
            position: absolute;
            right: 20px;
            top: 15px;
            cursor: pointer;
            transition: all 0.3s;
        }

        .close:hover {
            color: #666;
            transform: rotate(90deg);
        }

        /* 响应式设计 */
        @media (max-width: 768px) {
            .dashboard-container {
                padding: 25px;
            }

            .modal-content {
                width: 90%;
                margin: 20% auto;
                padding: 24px;
            }

            .contract-button {
                width: 100%;
                justify-content: center;
            }
        }
    </style>
</head>
<body>
    <div class="button-group">
        <button onclick="openModal('hardModal')" class="contract-button">
            📝 签署新硬广合同
        </button>
        <button onclick="openModal('softModal')" class="contract-button">
            📝 签署软广合同
        </button>
        <a href="{% url 'sourcematerial:material_library' %}" class="contract-button">
            📂 查看素材库
        </a>
    </div>
        <div class="divider"></div>

        <h3>📋 已签署硬广合同</h3>
        {% if hard_contracts %}
            <table>
                <thead>
                    <tr>
                        <th>提交日期</th>
                        <th>开始时间</th>
                        <th>结束时间</th>
                        <th>广告位置</th>
                        <th>合同状态</th>
                    </tr>
                </thead>
                <tbody>
                    {% for c in hard_contracts %}
                    <tr>
                        <td>{{ c.sign_date }}</td>
                        <td>{{ c.start_time }}</td>
                        <td>{{ c.end_time }}</td>
                        <td>{{ c.site }}</td>
                        <td>
                            <span class="status-indicator">
                                {{ c.status }}
                            </span>
                        </td>
                    </tr>
                    {% endfor %}
                </tbody>
            </table>
        {% else %}
            <div class="empty-state">📭 你还没有签署任何硬广合同</div>
        {% endif %}

        <div class="divider"></div>

        <h3>📋 已签署软广合同</h3>
        {% if soft_contracts %}
            <table>
                <thead>
                    <tr>
                        <th>提交日期</th>
                        <th>开始时间</th>
                        <th>结束时间</th>
                        <th>媒体平台</th>
                        <th>需求描述</th>
                    </tr>
                </thead>
                <tbody>
                    {% for c in soft_contracts %}
                    <tr>
                        <td>{{ c.sign_date }}</td>
                        <td>{{ c.start_time }}</td>
                        <td>{{ c.end_time }}</td>
                        <td>{{ c.media }}</td>
                        <td>{{ c.need|truncatechars:30 }}</td>
                    </tr>
                    {% endfor %}
                </tbody>
            </table>
        {% else %}
            <div class="empty-state">📭 你还没有签署软广合同</div>
        {% endif %}
    </div>
    <div id="hardModal" class="modal">
        <div class="modal-content">
            <span class="close" onclick="closeModal('hardModal')">&times;</span>
            <h3>签署硬广合同</h3>
            <form method="post" action="{% url 'hard_contract_form' %}">
                {% csrf_token %}
                <div class="form-group">
                    <label>开始时间<span class="required">*</span></label>
                    <input type="date" name="start_time" required>
                </div>
                <div class="form-group">
                    <label>结束时间<span class="required">*</span></label>
                    <input type="date" name="end_time" required>
                </div>
                <div class="form-group">
                    <label>广告位置<span class="required">*</span></label>
                    <select name="site" required>
                        <option value="">-- 请选择 --</option>
                        <option value="首页顶部通栏">首页顶部通栏</option>
                        <option value="侧边栏广告位">侧边栏广告位</option>
                        <option value="文章页底部">文章页底部</option>
                        <option value="移动端首屏">移动端首屏</option>
                        <option value="视频前贴片">视频前贴片</option>
                    </select>
                </div>
                <div class="form-group">
                    <label>文案内容<span class="required">*</span></label>
                    <textarea name="content" required></textarea>
                </div>
                <button type="submit">提交合同</button>
            </form>
        </div>
    </div>

    <!-- 软广合同模态框 -->
    <div id="softModal" class="modal">
        <div class="modal-content">
            <span class="close" onclick="closeModal('softModal')">&times;</span>
            <h3>签署软广合同</h3>
            <form method="post" action="{% url 'soft_contract_form' %}">
                {% csrf_token %}
                <div class="form-group">
                    <label>开始时间<span class="required">*</span></label>
                    <input type="date" name="start_time" required>
                </div>
                <div class="form-group">
                    <label>结束时间<span class="required">*</span></label>
                    <input type="date" name="end_time" required>
                </div>
                <div class="form-group">
                    <label>媒体平台<span class="required">*</span></label>
                    <select name="media" required>
                        <option value="">-- 请选择 --</option>
                        <option value="抖音">抖音</option>
                        <option value="小红书">小红书</option>
                        <option value="视频号">视频号</option>
                    </select>
                </div>
                <div class="form-group">
                    <label>需求描述<span class="required">*</span></label>
                    <textarea name="need" required></textarea>
                </div>
                <button type="submit">提交合同</button>
            </form>
        </div>
    </div>
</div>

<script>
    // 模态框控制逻辑
    function openModal(id) {
        document.getElementById(id).style.display = 'block';
    }

    function closeModal(id) {
        document.getElementById(id).style.display = 'none';
    }

    // 点击背景关闭模态框
    window.onclick = function(event) {
        if (event.target.classList.contains('modal')) {
            event.target.style.display = 'none';
        }
    }

    // 表单验证增强
    document.querySelectorAll('form').forEach(form => {
        form.addEventListener('submit', (e) => {
            const requiredFields = form.querySelectorAll('[required]');
            let isValid = true;

            requiredFields.forEach(field => {
                if (!field.value.trim()) {
                    field.style.borderColor = '#e74c3c';
                    isValid = false;
                }
            });

            if (!isValid) {
                e.preventDefault();
                alert('请填写所有必填字段（标*项）');
            }
        });
    });
</script>
</body>
</html>
{% endblock %}